package cn.zwz.home.controller;

import cn.zwz.basics.utils.PageUtil;
import cn.zwz.basics.utils.ResultUtil;
import cn.zwz.basics.baseVo.PageVo;
import cn.zwz.basics.baseVo.Result;
import cn.zwz.basics.utils.SecurityUtil;
import cn.zwz.data.utils.ZwzNullUtils;
import cn.zwz.home.entity.Student;
import cn.zwz.home.entity.Task;
import cn.zwz.home.service.IStudentService;
import cn.zwz.home.service.ITaskService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 郑为中
 */
@Slf4j
@RestController
@Api(tags = "作业管理接口")
@RequestMapping("/zwz/task")
@Transactional
public class TaskController {

    @Autowired
    private ITaskService iTaskService;

    @Autowired
    private IStudentService iStudentService;

    @Autowired
    private SecurityUtil securityUtil;

    @RequestMapping(value = "/getOne", method = RequestMethod.GET)
    @ApiOperation(value = "查询单条作业")
    public Result<Task> get(@RequestParam String id){
        return new ResultUtil<Task>().setData(iTaskService.getById(id));
    }

    @RequestMapping(value = "/count", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部作业个数")
    public Result<Long> getCount(){
        return new ResultUtil<Long>().setData(iTaskService.count());
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "查询全部作业")
    public Result<List<Task>> getAll(){
        return new ResultUtil<List<Task>>().setData(iTaskService.list());
    }

    @RequestMapping(value = "/getByPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询作业")
    public Result<IPage<Task>> getByPage(@ModelAttribute Task task ,@ModelAttribute PageVo page){
        QueryWrapper<Task> qw = new QueryWrapper<>();
        if(!ZwzNullUtils.isNull(task.getStudentId())) {
            qw.eq("student_id",task.getStudentId());
        }
        if(!ZwzNullUtils.isNull(task.getTitle())) {
            qw.like("title",task.getTitle());
        }
        IPage<Task> data = iTaskService.page(PageUtil.initMpPage(page),qw);
        for (Task ta : data.getRecords()) {
            if(!ZwzNullUtils.isNull(ta.getStudentId())) {
                Student stu = iStudentService.getById(ta.getStudentId());
                if(stu != null) {
                    ta.setStudentName(stu.getUserName());
                }
            }
        }
        return new ResultUtil<IPage<Task>>().setData(data);
    }

    @RequestMapping(value = "/getByMyPage", method = RequestMethod.GET)
    @ApiOperation(value = "查询作业")
    public Result<IPage<Task>> getByMyPage(@ModelAttribute Task task ,@ModelAttribute PageVo page){
        QueryWrapper<Task> qw = new QueryWrapper<>();
        qw.eq("student_id",securityUtil.getCurrUser().getStudentId());
        if(!ZwzNullUtils.isNull(task.getTitle())) {
            qw.like("title",task.getTitle());
        }
        IPage<Task> data = iTaskService.page(PageUtil.initMpPage(page),qw);
        for (Task ta : data.getRecords()) {
            if(!ZwzNullUtils.isNull(ta.getStudentId())) {
                Student stu = iStudentService.getById(ta.getStudentId());
                if(stu != null) {
                    ta.setStudentName(stu.getUserName());
                }
            }
        }
        return new ResultUtil<IPage<Task>>().setData(data);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "增改作业")
    public Result<Task> saveOrUpdate(Task task){
        if(iTaskService.saveOrUpdate(task)){
            return new ResultUtil<Task>().setData(task);
        }
        return ResultUtil.error();
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @ApiOperation(value = "新增作业")
    public Result<Task> insert(Task task){
        if(ZwzNullUtils.isNull(task.getStudentId())) {
            return ResultUtil.error("学生不能为空");
        }
        Student stu = iStudentService.getById(task.getStudentId());
        if(stu == null) {
            return ResultUtil.error("学生不存在");
        }
        iTaskService.saveOrUpdate(task);
        return new ResultUtil<Task>().setData(task);
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @ApiOperation(value = "编辑作业")
    public Result<Task> update(Task task){
        if(ZwzNullUtils.isNull(task.getStudentId())) {
            return ResultUtil.error("学生不能为空");
        }
        Student stu = iStudentService.getById(task.getStudentId());
        if(stu == null) {
            return ResultUtil.error("学生不存在");
        }
        iTaskService.saveOrUpdate(task);
        return new ResultUtil<Task>().setData(task);
    }

    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @ApiOperation(value = "删除作业")
    public Result<Object> delByIds(@RequestParam String[] ids){
        for(String id : ids){
            iTaskService.removeById(id);
        }
        return ResultUtil.success();
    }
}
